package com.lwj.exception;

import com.lwj.controller.UserController;
import com.lwj.entity.RestEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
class ExceptionHandlerAdvice {
    private static final Logger log = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);

    @ResponseStatus(HttpStatus.UNAUTHORIZED)  // 401
    @ExceptionHandler(NoPermissionException.class)
    public @ResponseBody
    RestEntity handleNoPermissionException(NoPermissionException e) {
        log.error("你没有权限",e.fillInStackTrace());
        return RestEntity.error("你没有权限");
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)  // 500
    @ExceptionHandler(BusinessException.class)
    public @ResponseBody
    RestEntity handleBusinessException(BusinessException e) {
        log.error(e.getMessage(),e.fillInStackTrace());
        return RestEntity.error(e.getMessage());
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)  // 500
    @ExceptionHandler(Exception.class)
    public @ResponseBody
    RestEntity handleException(Exception e) {
        log.error("未知错误",e);
        return RestEntity.error("未知错误");
    }
}